#!/usr/bin/env bash
#-------------------------------------------------------
#	FileName	: splt.sh
#	Author		：hpy
#	Date		：2023年03月19日
#	Description	：专用于绘制光谱图，光谱文件第一列为x，后面
#                 每一列代表一个光谱
#-------------------------------------------------------
# out="png" 

for i in $* 
do 
    cd $(dirname $i)
    test ! -f $i && "echo file $i is't exist" && continue 
    fid=$i 
    col=$(cat $i | awk '{if($0 ~ /^#.*/ || NF < 1) next ; print NF; exit}') 
    if [ $col -lt 2 ] ; then 
        echo "file $i have error!" && continue 
    fi 
    pfile="${i%.*}.plt"
    if [ -f $fid.$out ] ; then 
        cd -
        continue 
    fi 
    printf "#!/usr/bin/env gnuplot
unset grid  #设置网格
unset key
# set term $out 
# set output \"$fid.$out\"
set xlabel \"x_dis_lab\"
set ylabel \"y_dis_lab\" 
" > $pfile

    str="plot "
    j=2
    while ((j<=$col)) 
    do 
        str="$str \"$i\" u 1:$j w l t \"$j\","
        ((j=j+1))
    done 
    str=${str%,*}
    echo $str >> $pfile 
    # echo "pause 600" >> $pfile 
    # echo "exit" >> $pfile 
    echo "pause -1" >> $pfile 

    gnuplot -c $pfile &
    cd -
done 
